﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mimosa_web.Biz;

namespace mimosa_web
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            mimosa_web.Storage.DBManager.LoadScript(Request.PhysicalApplicationPath + "Scripts\\sql.xml");
            this.Response.ContentType = "text/xml;charset=UTF-8";
            this.Response.Write(processRequest());
        }

        protected string processRequest()
        {
            //Response.SetContentType("text/xml;charset=UTF-8");
            param[] parameterArry = null;
            try
            {
                String szFuncType = Request.Params["func_type"];//"set_mac_address";//
                String szOpType = Request.Params["op_tag"];//"{\"acct_id\":\"0x0001\",\"mac\":[\"A071A9BD2567\",\"A071A9BAABB6\",\"A071A9BD258F\",\"A071A9BA9471\"]}";// 
                String szParams = Request.Params["params"];
                if (szParams != null && szParams.Length > 0)
                {
                    string[] parameters = szParams.Split(new char[] { ',' });
                    String value = "";
                    if (parameters.Length > 0)
                    {
                        parameterArry = new param[parameters.Length];
                        for (int i = 0; i < parameters.Length; i++)
                        {
                            parameterArry[i] = new param();
                            parameterArry[i].setDataType(1);
                            value = (parameters[i].Equals("NULL")) ? "" : parameters[i];
                            parameterArry[i].setValue(value);
                        }
                    }
                }
                

                bool result = true;
                string resultString = "", errorString = "";
                if (szFuncType == string.Empty && szOpType != string.Empty)
                {
                    if (!UserMng.GetData(szOpType, parameterArry, ref resultString))
                    {
                        result = false;
                        errorString = resultString;
                    }
                }

                // Transaction 처리 
                if (szFuncType != string.Empty && szOpType != string.Empty)
                {
                    switch (szFuncType)
                    {
                        case "create_account":
                            if (!UserMng.CreateAccount(szOpType, parameterArry, ref resultString))
                            {
                                result = false;
                                errorString = resultString;
                            }
                            szOpType = szFuncType;
                            break;
                        case "delete_account":
                            if (!UserMng.DeleteAccount(szOpType, parameterArry, ref resultString))
                            {
                                result = false;
                                errorString = resultString;
                            }
                            szOpType = szFuncType;
                            break;
                        case "set_mac_address":
                            if (!UserMng.UpdateSysContract(szOpType, parameterArry, ref resultString))
                            {
                                result = false;
                                errorString = resultString;
                            }
                            szOpType = szFuncType;
                            break;
                        case "call_time":
                            if (!UserMng.SetCallTime(szOpType, parameterArry, ref resultString))
                            {
                                result = false;
                                errorString = resultString;
                            }
                            szOpType = szFuncType;
                            break;
                        default:
                            break;
                    }
                }

                String szXMLRslt = string.Format("<enc_result><result>{0}</result><error>{1}</error><command>{2}</command><data><![CDATA[{3}]]></data></enc_result>", result, errorString, szOpType, resultString);
                return szXMLRslt;
            }
            catch (Exception e)
            {
                Console.WriteLine("<error>");
                Console.WriteLine(e.StackTrace);
                Console.WriteLine("</error>");
            }
            return string.Empty;

        }
    }
    
    public class param 
    {
      protected int     m_dataType = 0;  //0: int, 1: string, 10:치환문자열
      protected String  m_value = "";
      protected String  m_name = "";
      protected bool m_null = false;
      public param()
      {
      }
  
      public param(int dataType, String name, String value) //chan0809
      {
          m_dataType = dataType;
          m_name = name;
          m_value = value;
      }
  
      public void setDataType( int dataType ) { m_dataType = dataType; }
      public int getDataType(){ return m_dataType; }
  
      public void setValue( String value ) { m_value = value; }
      public String getValue(){ return m_value; }
  
      public void setName( String name ) { m_name = name; }
      public String getName(){ return m_name; }

      public void setNull( bool bnull ) { m_null = bnull; }
      public bool isNull(){ return m_null; }
    }
}